home *** CD-ROM | disk | FTP | other *** search
/ Programming Windows (5th Edition) / Programming Windows, 5th ed. - Companion CD (097-0002183)(1999).iso / Chap05 / RandRect / RandRect.c next >
Encoding:
C/C++ Source or Header  |  1998-10-09  |  3.0 KB  |  101 lines

  1. /*------------------------------------------
  2.    RANDRECT.C -- Displays Random Rectangles
  3.                  (c) Charles Petzold, 1998
  4.   ------------------------------------------*/
  5.  
  6. #include <windows.h>
  7. #include <stdlib.h>           // for the rand function
  8.  
  9. LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
  10. void DrawRectangle (HWND) ;
  11.  
  12. int cxClient, cyClient ;
  13.  
  14. int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
  15.                     PSTR szCmdLine, int iCmdShow)
  16. {
  17.      static TCHAR szAppName[] = TEXT ("RandRect") ;
  18.      HWND         hwnd ;
  19.      MSG          msg ;
  20.      WNDCLASS     wndclass ;
  21.      
  22.      wndclass.style         = CS_HREDRAW | CS_VREDRAW ;
  23.      wndclass.lpfnWndProc   = WndProc ;
  24.      wndclass.cbClsExtra    = 0 ;
  25.      wndclass.cbWndExtra    = 0 ;
  26.      wndclass.hInstance     = hInstance ;
  27.      wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION) ;
  28.      wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
  29.      wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
  30.      wndclass.lpszMenuName  = NULL ;
  31.      wndclass.lpszClassName = szAppName ;
  32.      
  33.      if (!RegisterClass (&wndclass))
  34.      {
  35.           MessageBox (NULL, TEXT ("This program requires Windows NT!"),
  36.                       szAppName, MB_ICONERROR) ;
  37.           return 0 ;
  38.      }
  39.      
  40.      hwnd = CreateWindow (szAppName, TEXT ("Random Rectangles"),
  41.                           WS_OVERLAPPEDWINDOW,
  42.                           CW_USEDEFAULT, CW_USEDEFAULT,
  43.                           CW_USEDEFAULT, CW_USEDEFAULT,
  44.                           NULL, NULL, hInstance, NULL) ;
  45.      
  46.      ShowWindow (hwnd, iCmdShow) ;
  47.      UpdateWindow (hwnd) ;
  48.      
  49.      while (TRUE)
  50.      {
  51.           if (PeekMessage (&msg, NULL, 0, 0, PM_REMOVE))
  52.           {
  53.                if (msg.message == WM_QUIT)
  54.                     break ;
  55.                
  56.                TranslateMessage (&msg) ;
  57.                DispatchMessage (&msg) ;
  58.           }
  59.           else
  60.                DrawRectangle (hwnd) ;
  61.      }
  62.      return msg.wParam ;
  63. }
  64.  
  65. LRESULT CALLBACK WndProc (HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam)
  66. {
  67.      switch (iMsg)
  68.      {
  69.      case WM_SIZE:
  70.           cxClient = LOWORD (lParam) ;
  71.           cyClient = HIWORD (lParam) ;
  72.           return 0 ;
  73.           
  74.      case WM_DESTROY:
  75.           PostQuitMessage (0) ;
  76.           return 0 ;
  77.      }
  78.      return DefWindowProc (hwnd, iMsg, wParam, lParam) ;
  79. }
  80.  
  81. void DrawRectangle (HWND hwnd)
  82. {
  83.      HBRUSH hBrush ;
  84.      HDC    hdc ;
  85.      RECT   rect ;
  86.      
  87.      if (cxClient == 0 || cyClient == 0)
  88.           return ;
  89.      
  90.      SetRect (&rect, rand () % cxClient, rand () % cyClient,
  91.                      rand () % cxClient, rand () % cyClient) ;
  92.      
  93.      hBrush = CreateSolidBrush (
  94.                     RGB (rand () % 256, rand () % 256, rand () % 256)) ;
  95.  
  96.      hdc = GetDC (hwnd) ;
  97.      FillRect (hdc, &rect, hBrush) ;
  98.      ReleaseDC (hwnd, hdc) ;
  99.      DeleteObject (hBrush) ;
  100. }     
  101.